<?php
require_once ("../src/db/connectdatabase.php");
function getRateBasicPros($bank, $start, $lenght) {
	$con = new ConnectDB();
	
	$sql = "Select r.basic_id, r.bankid, b.bankname, b.banklogo, r.basic_rate1, r.basic_rate2, r.datecreate, r.basic_isuse FROM rate_basic_mortgage as r, banks as b WHERE r.bankid = b.bankid AND r.bankid LIKE '%".$bank."%' ORDER BY r.datecreate DESC LIMIT ".$start.",".$lenght;
	return $con -> getvalueString($sql);
}

function getRowRateBasicPro($bank) {
	$con = new ConnectDB();
	
	$sql = "Select COUNT(*) FROM rate_basic_mortgage WHERE bankid LIKE '%".$bank."%'";
	return $con -> getvalueString($sql);
}

function int_to_date($int)
{
    $time  = date("d/m/Y", $int);
    return $time;
}
$bank = "";

if(isset($_GET['bank'])) {
	$bank = $_GET['bank'];
}

$page = 1;
if(isset($_GET['page'])) {
	$page = $_GET['page'];
}

$arrrows = getRowRateBasicPro($bank);

$sumrecord = $arrrows[0][0];
$numrecord = 50;
$pageNumber = intval(($sumrecord / $numrecord));
if(($sumrecord % $numrecord) != 0) {
	$pageNumber++;
}

if($pageNumber == 0) {
	$pageNumber = 1;	
}

if($page > $pageNumber) {
	$page = $pageNumber;
}

//load data
$arrdata = getRateBasicPros($bank, ($page-1)*$numrecord, $numrecord);

?>

<style>
.listviewcontent table {
	width: 900px;
	max-width: 900px;
}

.listviewcontent table td {
	margin-top: 2px;
	border: 1px solid;
	border-radius: 2px 2px 2px 2px;
}

.title {
	text-align: center;
	background-color: #0088cc;
	min-height: 40px;
	-moz-border-radius: 8px 8px 0px 0px;
	-webkit-border-radius: 8px 8px 0px 0px;
	border-radius: 8px 8px 0px 0px;
	color: #FFF;
}

.listviewcontent table tr:hover td {
	background: #FFC;
}

.class-pagination{
		float: right;
		clear: left;
		padding-top: 20px;
	}
	
	.class-pageLabel{
		font-size: 11px;
		font-weight: bold;
		text-transform: uppercase;
	}

	.class-pageNow{
		padding: 5px;
		padding-top: 4px;
		margin: 2px;
		font-size: 11px;
		font-weight: bold;
		border: 1px solid #C0C0C0;
		background-color: #C0C0C0;
		border-radius: 4px;
		box-shadow: 0 0 10px rgba(0,0,0,.1);
		cursor: default;
	}
	
	.class-pageElem{
		padding: 5px;
		padding-top: 4px;
		margin: 2px;
		font-size: 11px;
		font-weight: bold;
		border: 1px solid #C0C0C0;
		box-shadow: 0 0 10px rgba(0,0,0,.2);
		border-radius: 4px;
		background-color: white;
	}

	.class-pageElem:hover{
		padding: 5px;
		margin: 2px;
		font-size: 11px;
		font-weight: bold;
		background-color: #C0C0C0;
		cursor: pointer;
	}
	.submitform {
		background: #004A00;
		text-indent: 1px;
		width:80px;
		height:30px;
		cursor: pointer;
		font-weight: bold;
		font-size: 14px;
	}
	select {
		width: 270px;
		height: 30px;
		padding: 5px;
		margin-left: 5px;
		margin-right: 5px;
		margin-bottom: 5px;
	}
</style>
<h3>Danh Sách Lãi Suất Cơ Bản Thế Chấp</h3>
<div>
	<div style="width:600px; float:left;">	
    	<table>
        	<tr>
                <td>Ngân hàng</td>
                <td><select class="selectbank" name="selectbank"><option value="">--Tất cả--</option></select></td>
                <td><input id="form_submitsearch" type="button" style="cursor:pointer ;background-image:url(../images/timkiem.png); width:70px; height:24px; border:0px;" value="" /></td>
                <td></td>
                <td></td>
            </tr>
        </table>
    </div>
    <div style="clear:both; height:15px;"></div>
    <div><a href="admin.php?content=quanlivay&&as=vay_the_chap&&adrt=add-lai-suat-cb-the-chap"><img title="Thêm lãi suất cơ bản vay thế chấp" src="../images/icon-add.jpg"/> Thêm lãi suất cơ bản vay thế chấp</a></div>
    <div style="clear:both;"></div>
    <div style="float:right;">
    	<strong>Chú Thích:</strong> <img src="../images/unclockicon.png" title="Được phép hiển thị" /> Hiển thị &nbsp;&nbsp;<img src="../images/lockicon.png" title="Không được phép hiển thị" /> Ẩn
    </div>
    <div style="clear:both;"></div>
    <div class="class-pagination">
        <span class="class-pageLabel">Trang</span>
        <?php if($page == 1) { ?>
        <span title="Trang 1" page="1" class="class-pageNow">1</span>
        <?php } else { ?>
        <span title="Trang 1" page="1" class="class-pageElem">1</span>
        <?php }?>
        <?php if ($pageNumber != 1) { for($i = 2; $i <= $pageNumber; $i++) { ?>
        <?php if($i == $page) { ?>
        <span title="Trang <?php echo($i) ?>" page="<?php echo($i) ?>" class="class-pageNow"><?php echo($i) ?></span>
        <?php } else { ?>
        <span title="Trang <?php echo($i) ?>" page="<?php echo($i) ?>" class="class-pageElem"><?php echo($i) ?></span>
        <?php }?>
        <?php }}?>
    </div>
    <div style="clear:both; height:10px;"></div>
    
    <div class="listviewcontent">
    	<table>
        	<tr>
            	<th class="title" width="40px">STT</th>
                <th class="title" width="200px">Ngân hàng</th>
            	<th class="title" width="200px">Lãi suất</th>
                <th class="title" width="180px" style="max-width: 180px;">Ngày C.nhật</th>
                <th class="title">Chức Năng</th>
            </tr>
            <?php
			$i = ($page-1)*$numrecord + 1;
			foreach($arrdata as $entry) {
			?>
            <tr>
            	<td align="center"><?php echo($i) ?></td>
                <td align="center"><img width="90px" title="<?php echo($entry[2]) ?> (<?php echo($entry[1]) ?>)" src="../<?php echo($entry[3]) ?>"></img></td>
            	<td align="center"><?php echo($entry[4]) ?>% - <?php echo($entry[5]) ?>%</td>
                <td align="center"><?php echo(int_to_date($entry[6])) ?></td>
                <td align="center"><?php if($entry[7] == "1") { echo('<img src="../images/unclockicon.png" style="cursor:pointer;" title="Click để không cho hiển thị" class="class-lockacc-link" idacc="'.$entry[0].'" />'); } else { echo('<img src="../images/lockicon.png" style="cursor:pointer;" title="Click để cho phép hiển thị" class="class-unlockacc-link" idacc="'.$entry[0].'" />'); } ?> | <a href="admin.php?content=quanlivay&&as=vay_the_chap&&adrt=edit-lai-suat-cb-the-chap&id=<?php echo($entry[0]) ?>"><img style="cursor:pointer;" title="Xem Chi Tiết Thông Tin" src="../images/icon-edit.gif" width="20px"></a></td>
            </tr>
            <?php
				$i++;
			}
			?>
        </table>
    </div>
    
    <div style="clear:both;"></div>
    <div class="class-pagination">
        <span class="class-pageLabel">Trang</span>
        <?php if($page == 1) { ?>
        <span title="Trang 1" page="1" class="class-pageNow">1</span>
        <?php } else { ?>
        <span title="Trang 1" page="1" class="class-pageElem">1</span>
        <?php }?>
        <?php if ($pageNumber != 1) { for($i = 2; $i <= $pageNumber; $i++) { ?>
        <?php if($i == $page) { ?>
        <span title="Trang <?php echo($i) ?>" page="<?php echo($i) ?>" class="class-pageNow"><?php echo($i) ?></span>
        <?php } else { ?>
        <span title="Trang <?php echo($i) ?>" page="<?php echo($i) ?>" class="class-pageElem"><?php echo($i) ?></span>
        <?php }?>
        <?php }}?>
    </div>
    <div style="clear:both; height:15px;"></div>
    <div><a href="admin.php?content=quanlivay&&as=vay_the_chap&&adrt=add-lai-suat-cb-the-chap"><img title="Thêm lãi suất cơ bản vay thế chấp" src="../images/icon-add.jpg"/> Thêm lãi suất cơ bản vay thế chấp</a></div>
    <div style="clear:both;"></div>
</div>

<script>
loadbank();
function loadbank() {
	$.ajax({
		url: "../src/ajax/ajaxbank.php",
		type: "post",
		data: {act:'getbanks'},
		dataType:"json",
		success: function(response) {
			html = '';
			$.each(response, function(entryIndex, entry){
				html += '<option value="' + entry[0] + '">' + entry[1] + '(' + entry[0] + ')</option>';
			});
			$('.selectbank').append(html);
			$(".selectbank option[value='<?php echo($bank) ?>']").attr('selected', 'selected');
		}
	});
}

    $('#form_submitsearch').click(function(e) {
        var bank = $('.selectbank').val();
		
		openwindowns('admin.php?content=quanlivay&as=vay_the_chap&adrt=lai-suat-cb-the-chap&bank=' + bank);
    });
	
	$(document).on('click', '.class-pageElem', function() {
		page = $(this).attr('page');
		
		var bank = $('.selectbank').val();
		
		openwindowns('admin.php?content=quanlivay&as=vay_the_chap&adrt=lai-suat-cb-the-chap&bank=' + bank + '&page=' + page);
	});
	
	function openwindowns(url) {
		window.location = url;
	}
	
	$(document).on('click', '.class-lockacc-link', function() {
		elem = $(this);
		idacc = elem.attr('idacc');
		$.ajax({
			url: "../src/ajax/ajaxRateBasicMort.php",
			type: "post",
			data: {act:'lock', idre: idacc},
			dataType:"json",
			success: function(data) {
				if(data == true) {
					alert("Cập nhật dữ liệu thành công");
					location.reload();
				} else {
					alert("Cập nhật dữ liệu thất bại");
				}
			}
		});
	});
	
	$(document).on('click', '.class-unlockacc-link', function() {
		elem = $(this);
		idacc = elem.attr('idacc');
		$.ajax({
			url: "../src/ajax/ajaxRateBasicMort.php",
			type: "post",
			data: {act:'unlock', idre: idacc},
			dataType:"json",
			success: function(data) {
				if(data == true) {
					alert("Cập nhật dữ liệu thành công");
					location.reload();
				} else {
					alert("Cập nhật dữ liệu thất bại");
				}
			}
		});
	});
</script>